﻿#pragma once
#include "Defs.h"
#include <QWidget>
#include <QPoint>
#include <QComboBox>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QPushButton>
#include "SearchResultList.h"
#include "ThemeManager.h"

class CodeEditor;

class SuspendedSearchBox : public QWidget
{
    Q_OBJECT
public:
    explicit SuspendedSearchBox(CodeEditor* parentEditor = nullptr, QWidget* parent = nullptr);

    void setFindMode(FindMode mode);
    FindMode getCurrentFindMode() const;
    QString getSearchText() const;
    void showSearchBox();
    void hideSearchBox();
    void showResults();
    void hideResults();

signals:
    void hidden();

protected:
    void paintEvent(QPaintEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void closeEvent(QCloseEvent* event) override;
    void keyPressEvent(QKeyEvent* event) override;

private slots:
    void onFindButtonClicked();
    void onFindModeChanged(int index);
    void snapToEditorEdge();
    void onCloseButtonClicked();
    void onContentSearchCompleted(int count, bool isMultiFile);

    // ===== 主题相关槽函数 =====
    void onThemeChanged(ThemeManager::Theme newTheme);

private:
    // ===== 主题相关方法 =====
    void applyCurrentTheme();
    void applyLightTheme();
    void applyDarkTheme();

    CodeEditor* m_parentEditor;
    QWidget* m_contentWidget;     // 新增：内容区域控件指针
    QLineEdit* m_searchEdit;
    QComboBox* m_modeCombo;
    QPushButton* m_findButton;
    QPushButton* m_closeButton;

    QRect m_titleBarRect;
    const int m_titleBarHeight = 24;

    SearchResultList* m_resultList;
    bool m_resultsVisible = false;

    QPoint m_dragStartPos;
    bool m_isDragging;
    const int m_snapDistance = 8;
    const int m_windowMargin = 10;
    const int m_contentMargin = 6;
};